package com.amumu.drama.admin.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.json.JSONUtil;
import com.amumu.drama.common.utils.MD5Utils;
import com.amumu.drama.admin.response.FuluResp;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.*;

/**
 * 福禄开放平台
 * http://openapi.fulu.com/api/getway
 */
@Slf4j
public class FuluApiReqClient {

    @Value("${remote-fulu.url:http://openapi.fulu.com/api/getway}")
    private String aiAgentUrl;

    @Value("${remote-fulu.textEmbeddingUrl:fulu.goods.list.get}")
    private String getGoodsList;

    private static RestTemplate restTemplate;

    static {
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(5 * 1000);
        clientHttpRequestFactory.setReadTimeout(60 * 1000);
        restTemplate = new RestTemplate(clientHttpRequestFactory);
    }



    /**
     * 获取商品列表接口
     * @return
     */
    public static String getFuluGoodsList() {
        TimeInterval timer = DateUtil.timer();
        try {
            timer.start();
            Map<String,String> map = Maps.newHashMap();
            map.put("app_key","hZoWVxglk3tg4Bb2OZ+ElIB1B2xF6/Jre3Shhs2/xgX/rFfMYW8coXgMr5HCQ9QN");
            map.put("method","fulu.goods.list.get");
            map.put("timestamp","2024-05-10 16:46:31");
            map.put("version","2.0");
            map.put("sign",sign("55e7283678e54939b9830362fc5423fb"));
            map.put("format","json");
            map.put("charset","utf-8");
            map.put("sign_type","md5");
            map.put("app_auth_token","");
            map.put("biz_content","{\"third_category_id\":46998}");
            FuluResp fuluResp = restTemplate.postForObject(new URI("http://openapi.fulu.com/api/getway"), map, FuluResp.class);
            if (fuluResp.isOk()) {
                return fuluResp.getResult();
            } else {
                log.error("线程:{},获取商品列表接口接口异常：{}", Thread.currentThread().getName(),  JSONUtil.toJsonStr(fuluResp));
            }

        } catch (Exception e) {
            log.error("文本向量化接口:", e);
        } finally {
            log.debug("线程:{},获取商品列表接口,耗时:{}ms", Thread.currentThread().getName(), timer.intervalMs());
        }
        return null;
    }


    public static String sign(String sysSecret){
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_key","hZoWVxglk3tg4Bb2OZ+ElIB1B2xF6/Jre3Shhs2/xgX/rFfMYW8coXgMr5HCQ9QN");
        map.put("method","fulu.goods.list.get");
        map.put("timestamp","2024-05-10 16:46:31");
        map.put("version","2.0");
//        map.put("sign","55e7283678e54939b9830362fc5423fb");
        map.put("format","json");
        map.put("charset","utf-8");
        map.put("sign_type","md5");
        map.put("app_auth_token","");
        map.put("biz_content","{\"third_category_id\":46998}");

        // 将Map转换为JSON字符串
        String jsonStr = JSONUtil.toJsonStr(map);
        // 将JSON字符串转换为JSONObject
        char[] s = jsonStr.toCharArray();
        Arrays.sort(s);
        String outputSignOriginalStr = new String(s) + sysSecret;
        String sign = MD5Utils.MD5(outputSignOriginalStr);
        return sign;
    }
}






